package com.bls.sms.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

import com.bls.sms.dao.interfaces.ClassDAO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.wrapper.ClassWrapper;

public class ClassDAOImpl implements ClassDAO {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public int saveClass(ClassWrapper classWrapper) throws ApplicationException {
        try {
            this.sessionFactory.getCurrentSession().save(classWrapper);
        } catch (HibernateException hiberExcp) {
            throw new ApplicationException(hiberExcp.getMessage());
        }
        return 1;
    }
    public int updateClass(ClassWrapper classWrapper)
            throws ApplicationException {
        try {
            this.sessionFactory.getCurrentSession().update(classWrapper);
        } catch (HibernateException hiberExcp) {
            throw new ApplicationException(hiberExcp.getMessage());
        }
        return 1;
    }
    public int deleteClass(ClassWrapper classWrapper)
            throws ApplicationException {
        try {
            this.sessionFactory.getCurrentSession().delete(classWrapper);
        } catch (HibernateException hiberExcp) {
            throw new ApplicationException(hiberExcp.getMessage());
        }
        return 1;
    }
    public List<ClassWrapper> loadClasses() throws ApplicationException {
        try {
            return this.sessionFactory.getCurrentSession().createQuery(
                    "from ClassWrapper class").list();
        } catch (HibernateException e) {
            throw new ApplicationException(e.getMessage());
        }
    }
}
